package com.example.projecttest;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.example.projecttest.fragment.FourFragment;
import com.example.projecttest.fragment.OneFragment;
import com.example.projecttest.fragment.ThreeFragment;
import com.example.projecttest.fragment.TwoFragment;

public class FragmentTestActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnOne,btnTwo,btnThree,btnFour;
    private OneFragment oneFragment;
    private TwoFragment twoFragment;
    private ThreeFragment threeFragment;
    private FourFragment fourFragment;
    private FragmentTransaction fragmentTransaction1;
    private FragmentTransaction fragmentTransaction2;
    private FragmentTransaction fragmentTransaction3;
    private FragmentTransaction fragmentTransaction4;

    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        initView();
    }

    private void initView() {
        btnOne=$(R.id.btnOne);
        btnTwo=$(R.id.btnTwo);
        btnThree=$(R.id.btnThree);
        btnFour=$(R.id.btnFour);

        btnOne.setOnClickListener(this);
        btnTwo.setOnClickListener(this);
        btnThree.setOnClickListener(this);
        btnFour.setOnClickListener(this);

        oneFragment=new OneFragment();
        twoFragment=new TwoFragment();
        threeFragment=new ThreeFragment();
        fourFragment=new FourFragment();

        fragmentManager=getSupportFragmentManager();
        fragmentTransaction1=fragmentManager.beginTransaction();
        fragmentTransaction2=fragmentManager.beginTransaction();
        fragmentTransaction3=fragmentManager.beginTransaction();
        fragmentTransaction4=fragmentManager.beginTransaction();
        //add
        fragmentTransaction1.add(R.id.content_layout,oneFragment);
        fragmentTransaction1.commit();

        fragmentTransaction2.add(R.id.content_layout,twoFragment);
        fragmentTransaction2.commit();

        fragmentTransaction3.add(R.id.content_layout,threeFragment);
        fragmentTransaction3.commit();

        fragmentTransaction4.add(R.id.content_layout,fourFragment);
        fragmentTransaction4.commit();

        showHideFragment(oneFragment);
    }

    private void showHideFragment(Fragment fragment){
        FragmentTransaction fb=fragmentManager.beginTransaction();
        hideAllFragment(fb);
        fb.show(fragment);
        fb.commit();
    }

    //隐藏全部的Fragment
    private void hideAllFragment(FragmentTransaction fb) {
        if(oneFragment!=null){
            fb.hide(oneFragment);
        }

        if(twoFragment!=null){
            fb.hide(twoFragment);
        }

        if(threeFragment!=null){
            fb.hide(threeFragment);
        }

        if(fourFragment!=null){
            fb.hide(fourFragment);
        }
    }

    private void showFragment(Fragment fragment){
        FragmentManager fm=getSupportFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
        ft.replace(R.id.content_layout,fragment);
        ft.commit();
    }

    public <T> T $(int viewID){
        return (T) findViewById(viewID);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnOne:
                showHideFragment(oneFragment);
                break;

            case R.id.btnTwo:
                showHideFragment(twoFragment);
                break;
            case R.id.btnThree:
                showHideFragment(threeFragment);
                break;

            case R.id.btnFour:
                showHideFragment(fourFragment);
                break;
        }
    }
}
